package cn.wolfcode.service.impl;

import cn.wolfcode.domain.Classinfo;
import cn.wolfcode.mapper.ClassinfoMapper;
import cn.wolfcode.qo.PageResult;
import cn.wolfcode.qo.QueryObject;
import cn.wolfcode.service.IClassinfoService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.Collections;
import java.util.List;

@Service
public class ClassinfoServiceImpl implements IClassinfoService {
    @Autowired
    private ClassinfoMapper classinfoMapper;
    @Override
    public void delete(Long id) {
        classinfoMapper.deleteByPrimaryKey(id);

    }

    @Override
    public void save(Classinfo classinfo) {
        classinfoMapper.insert(classinfo);

    }

    @Override
    public Classinfo get(Long id) {
        return classinfoMapper.selectByPrimaryKey(id);
    }

    @Override
    public List<Classinfo> listAll() {
        return classinfoMapper.selectAll();
    }

    @Override
    public void update(Classinfo classinfo) {
        classinfoMapper.updateByPrimaryKey(classinfo);

    }

    @Override
    public PageResult<Classinfo> query(QueryObject qo) {
        int count = classinfoMapper.selectForCount(qo);
        if(count == 0){
            return  new PageResult<>(qo.getCurrentPage(),qo.getPageSize(),
                    Collections.emptyList(),count);
        }
        int a = count % qo.getPageSize() == 0 ? count / qo.getPageSize() : count / qo.getPageSize() + 1;
        if (qo.getCurrentPage() > a) {
            qo.setCurrentPage(a);
        }
        List<Classinfo> classinfos = classinfoMapper.selectForList(qo);
        return  new PageResult<>(qo.getCurrentPage(),qo.getPageSize(),
                classinfos,count);


    }
}
